#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
        vector<int> in(numCourses, 0);
        vector<vector<int>> edges(numCourses);
        for (auto& e : prerequisites)
        {
            int a = e[0];
            int b = e[1];
            edges[b].push_back(a);
            in[a]++;
        }

        queue<int> q;
        for (int i = 0; i < numCourses; ++i)
            if (in[i] == 0)
                q.push(i);

        while (q.size())
        {
            int tmp = q.front();
            q.pop();
            for (auto& e : edges[tmp])
            {
                in[e]--;
                if (in[e] == 0)
                {
                    q.push(e);
                }
            }
        }

        for (auto& e : in)
            if (e)
                return false;

        return true;
    }
};